#include "CustomThread.h"
#include "Scripting.h"
#include "../ScriptHook/Log.h"
#include <math.h>
#include <windows.h>

#define M_PI 3.14159265358979323846f


using namespace Scripting;

f32 wayPoints[232][3];
Vehicle rollerCoasterCar;
ch *currentText;
b8 modStarted=0;
b8 roundRunning=0;
Player mainPlayer;
Ped mainPed;
f32 currentX,currentY,currentZ,currentL,nextX,nextY,nextZ,nextL,doubleNextX,doubleNextY,doubleNextZ,doubleNextL,newX,newY,newZ,vectorX,vectorY,vectorZ,distance,distanceCounter,speed,rotX,rotZ,totalAngle,currentAngle,totalAngle2,currentAngle2,mX,mY,mZ,nX,nY,nZ,nL,radius,radius2,pX1,pY1,pZ1,pX2,pY2,pZ2,tX1,tY1,tZ1,tX2,tY2,tZ2;
u32 wayPointIndex=1;
Object modelObject, helpObject, helpObject2;


CustomThread::CustomThread()
{
	SetName("RollerCoasterMod");
}

void interpolate(u32 currentPosition)
{
	
}

void newPartialRoad()
{
	currentX = nextX;
	currentY = nextY;
	currentZ = nextZ;
	nextX=doubleNextX;
	nextY=doubleNextY;
	nextZ=doubleNextZ;
	wayPointIndex=(wayPointIndex+1)%232;
	doubleNextX = wayPoints[wayPointIndex][0];
	doubleNextY = wayPoints[wayPointIndex][1];
	doubleNextZ = wayPoints[wayPointIndex][2];
	vectorX = nextX-currentX;
	vectorY = nextY-currentY;
	vectorZ = nextZ-currentZ;
	distance=sqrt(pow(vectorX,2)+pow(vectorY,2)+pow(vectorZ,2));
	f32 p1X=nextX+currentX;
	f32 p1Y=nextY+currentY;
	f32 p2X=doubleNextX+nextX;
	f32 p2Y=doubleNextY+nextY;
	f32 v1X=currentY-nextY;
	f32 v1Y=nextX-currentX;
	f32 v2X=nextY-doubleNextY;
	f32 v2Y=doubleNextX-nextX;
	
	f32 l1=sqrt(pow(nextX-currentX,2)+pow(nextY-currentY,2));
	f32 l2=sqrt(pow(doubleNextX-nextX,2)+pow(doubleNextY-nextY,2));
	f32 z1 = nextZ-currentZ;
	f32 z2 = doubleNextZ-nextZ;
	
	f32 p1L = l1/2;
	f32 p1Z = z1/2;
	f32 p2L = l1+l2/2;
	f32 p2Z = z1+z2/2;
	f32 v1L = -z1;
	f32 v1Z = l1;
	f32 v2L = -z2;
	f32 v2Z = l2;

	f32 U = (v2Z*(p1L-p2L)-v2L*(p1Z-p2Z))/(v2L*v1Z-v1L*v2Z);

	f32 W = ((p2X-p1X)*v1Y-(p2Y-p1Y)*v1X)/(v2Y*v1X-v2X*v1Y);
	mX=(W*v2X+p2X)/2;
	mY=(W*v2Y+p2Y)/2;
	mZ=(nextZ+doubleNextZ)/2;

	nL=U*v1L+p1L;
	nX=nL*(nextX-currentX)/l1+currentX;
	nY=nL*(nextY-currentY)/l1+currentY;
	nZ=U*v1Z+p1Z+currentZ;


	SetObjectCoordinates(helpObject,mX,mY,mZ);
	SetObjectCoordinates(helpObject2,nX,nY,nZ);
	//AttachPedToObject(mainPed,helpObject2,0,0.0f,0.0f,0.0f,0.0f,0.0f,0,0);
	f32 rvP1X=currentX-mX;
	f32 rvP1Y=currentY-mY;
	f32 rvP2X=nextX-mX;
	f32 rvP2Y=nextY-mY;
	radius=sqrt(pow(rvP1X,2)+pow(rvP1Y,2));
	currentAngle=atan2(rvP1X,rvP1Y);
	totalAngle=atan2(rvP2X,rvP2Y)-currentAngle;	
	if(totalAngle>M_PI)
		totalAngle-=2*M_PI;
	else if(totalAngle<-M_PI)
		totalAngle+=2*M_PI;

	radius2=sqrt(pow(nX-currentX,2)+pow(nY-currentY,2)+pow(nZ-currentZ,2));
	currentAngle2=atan2(nL,U*v1Z+p1Z);
	totalAngle2=atan2(nL-l1,U*v1Z+p1Z-z1)-currentAngle2;	

	rotX=asin(vectorZ/distance)*180/M_PI;
	vectorX/=distance/speed;
	vectorY/=distance/speed;
	vectorZ/=distance/speed;
	currentX+=vectorY*2.2f;	//scale to middle of the road
	currentY-=vectorX*2.2f;	//" "			" "
	currentZ+=0.5f;			//scale a bit up
	distanceCounter=0.0f;
	rotZ=fmodf((atan2(-vectorX,vectorY)*180/M_PI),360.0f);
	
	currentX=mX+sin(currentAngle)*radius;
	currentY=mY+cos(currentAngle)*radius;
	newX=currentX;
	newY=currentY;

	pX1=currentX;
	pY1=currentY;
	pZ1=currentZ;
	pX2=nextX;
	pY2=nextY;
	pZ2=nextZ;

}

void CustomThread::RunScript()
{
	mainPlayer = ConvertIntToPlayerIndex(GetPlayerId());
	GetPlayerChar(mainPlayer, &mainPed);
	speed=0.05f;
	wayPoints[0][0]=1124.717773f;
	wayPoints[0][1]=-610.184509f;
	wayPoints[0][2]=13.308252f;
	wayPoints[1][0]=1118.187256f;
	wayPoints[1][1]=-607.987366f;
	wayPoints[1][2]=13.308315f;
	wayPoints[2][0]=1116.356323f;
	wayPoints[2][1]=-607.259338f;
	wayPoints[2][2]=13.308596f;
	wayPoints[3][0]=1114.870361f;
	wayPoints[3][1]=-606.565063f;
	wayPoints[3][2]=13.306417f;
	wayPoints[4][0]=1113.504395f;
	wayPoints[4][1]=-605.816711f;
	wayPoints[4][2]=13.307792f;
	wayPoints[5][0]=1112.594360f;
	wayPoints[5][1]=-605.125122f;
	wayPoints[5][2]=13.304947f;
	wayPoints[6][0]=1111.542847f;
	wayPoints[6][1]=-604.484497f;
	wayPoints[6][2]=13.308084f;
	wayPoints[7][0]=1110.597900f;
	wayPoints[7][1]=-603.543091f;
	wayPoints[7][2]=13.305930f;
	wayPoints[8][0]=1109.915161f;
	wayPoints[8][1]=-602.485901f;
	wayPoints[8][2]=13.309026f;
	wayPoints[9][0]=1109.440796f;
	wayPoints[9][1]=-601.223633f;
	wayPoints[9][2]=13.305871f;
	wayPoints[10][0]=1109.290405f;
	wayPoints[10][1]=-599.857300f;
	wayPoints[10][2]=13.307850f;
	wayPoints[11][0]=1109.386963f;
	wayPoints[11][1]=-598.797485f;
	wayPoints[11][2]=13.305713f;
	wayPoints[12][0]=1109.671875f;
	wayPoints[12][1]=-597.829956f;
	wayPoints[12][2]=13.309490f;
	wayPoints[13][0]=1110.126343f;
	wayPoints[13][1]=-596.886597f;
	wayPoints[13][2]=13.308343f;
	wayPoints[14][0]=1110.681396f;
	wayPoints[14][1]=-596.118347f;
	wayPoints[14][2]=13.306729f;
	wayPoints[15][0]=1111.410767f;
	wayPoints[15][1]=-595.407898f;
	wayPoints[15][2]=13.308738f;
	wayPoints[16][0]=1112.222046f;
	wayPoints[16][1]=-594.828613f;
	wayPoints[16][2]=13.309493f;
	wayPoints[17][0]=1113.107422f;
	wayPoints[17][1]=-594.411987f;
	wayPoints[17][2]=13.308237f;
	wayPoints[18][0]=1114.119751f;
	wayPoints[18][1]=-594.147217f;
	wayPoints[18][2]=13.306394f;
	wayPoints[19][0]=1115.168213f;
	wayPoints[19][1]=-594.045959f;
	wayPoints[19][2]=13.312943f;
	wayPoints[20][0]=1118.700928f;
	wayPoints[20][1]=-594.027222f;
	wayPoints[20][2]=13.891336f;
	wayPoints[21][0]=1122.349121f;
	wayPoints[21][1]=-594.203125f;
	wayPoints[21][2]=15.493823f;
	wayPoints[22][0]=1125.914185f;
	wayPoints[22][1]=-594.588867f;
	wayPoints[22][2]=17.794333f;
	wayPoints[23][0]=1129.443115f;
	wayPoints[23][1]=-595.173645f;
	wayPoints[23][2]=20.513285f;
	wayPoints[24][0]=1133.015625f;
	wayPoints[24][1]=-595.901062f;
	wayPoints[24][2]=23.422512f;
	wayPoints[25][0]=1136.642212f;
	wayPoints[25][1]=-596.811951f;
	wayPoints[25][2]=26.238546f;
	wayPoints[26][0]=1140.057373f;
	wayPoints[26][1]=-597.778564f;
	wayPoints[26][2]=28.477310f;
	wayPoints[27][0]=1143.593628f;
	wayPoints[27][1]=-598.905762f;
	wayPoints[27][2]=30.101761f;
	wayPoints[28][0]=1147.143188f;
	wayPoints[28][1]=-600.129089f;
	wayPoints[28][2]=30.688601f;
	wayPoints[29][0]=1150.565674f;
	wayPoints[29][1]=-601.382813f;
	wayPoints[29][2]=30.370163f;
	wayPoints[30][0]=1153.961792f;
	wayPoints[30][1]=-602.675903f;
	wayPoints[30][2]=29.533878f;
	wayPoints[31][0]=1157.225220f;
	wayPoints[31][1]=-603.947754f;
	wayPoints[31][2]=28.340813f;
	wayPoints[32][0]=1160.750000f;
	wayPoints[32][1]=-605.343445f;
	wayPoints[32][2]=26.732428f;
	wayPoints[33][0]=1171.277954f;
	wayPoints[33][1]=-609.465393f;
	wayPoints[33][2]=21.188717f;
	wayPoints[34][0]=1174.517456f;
	wayPoints[34][1]=-610.636475f;
	wayPoints[34][2]=19.594753f;
	wayPoints[35][0]=1178.077637f;
	wayPoints[35][1]=-611.898621f;
	wayPoints[35][2]=18.088274f;
	wayPoints[36][0]=1181.536499f;
	wayPoints[36][1]=-613.012512f;
	wayPoints[36][2]=17.238008f;
	wayPoints[37][0]=1185.009033f;
	wayPoints[37][1]=-614.052185f;
	wayPoints[37][2]=17.173466f;
	wayPoints[38][0]=1188.605347f;
	wayPoints[38][1]=-615.029968f;
	wayPoints[38][2]=17.724888f;
	wayPoints[39][0]=1192.173950f;
	wayPoints[39][1]=-615.852112f;
	wayPoints[39][2]=18.689283f;
	wayPoints[40][0]=1195.876465f;
	wayPoints[40][1]=-616.600830f;
	wayPoints[40][2]=19.920399f;
	wayPoints[41][0]=1199.493042f;
	wayPoints[41][1]=-617.158081f;
	wayPoints[41][2]=21.161102f;
	wayPoints[42][0]=1203.133911f;
	wayPoints[42][1]=-617.572754f;
	wayPoints[42][2]=22.261490f;
	wayPoints[43][0]=1207.090820f;
	wayPoints[43][1]=-617.843567f;
	wayPoints[43][2]=23.108425f;
	wayPoints[44][0]=1210.919067f;
	wayPoints[44][1]=-617.931824f;
	wayPoints[44][2]=23.416065f;
	wayPoints[45][0]=1213.800659f;
	wayPoints[45][1]=-617.673889f;
	wayPoints[45][2]=23.417398f;
	wayPoints[46][0]=1216.506348f;
	wayPoints[46][1]=-616.925293f;
	wayPoints[46][2]=23.416962f;
	wayPoints[47][0]=1218.982056f;
	wayPoints[47][1]=-615.739075f;
	wayPoints[47][2]=23.419033f;
	wayPoints[48][0]=1221.196899f;
	wayPoints[48][1]=-614.151001f;
	wayPoints[48][2]=23.417671f;
	wayPoints[49][0]=1223.115967f;
	wayPoints[49][1]=-612.227051f;
	wayPoints[49][2]=23.418987f;
	wayPoints[50][0]=1224.662109f;
	wayPoints[50][1]=-610.016663f;
	wayPoints[50][2]=23.417727f;
	wayPoints[51][0]=1225.842896f;
	wayPoints[51][1]=-607.501648f;
	wayPoints[51][2]=23.418554f;
	wayPoints[52][0]=1226.570557f;
	wayPoints[52][1]=-604.788452f;
	wayPoints[52][2]=23.416925f;
	wayPoints[53][0]=1226.829468f;
	wayPoints[53][1]=-601.946838f;
	wayPoints[53][2]=23.418776f;
	wayPoints[54][0]=1226.543701f;
	wayPoints[54][1]=-599.076782f;
	wayPoints[54][2]=23.521709f;
	wayPoints[55][0]=1225.806519f;
	wayPoints[55][1]=-596.375488f;
	wayPoints[55][2]=23.785828f;
	wayPoints[56][0]=1224.651001f;
	wayPoints[56][1]=-593.948486f;
	wayPoints[56][2]=24.151884f;
	wayPoints[57][0]=1223.049194f;
	wayPoints[57][1]=-591.728149f;
	wayPoints[57][2]=24.597752f;
	wayPoints[58][0]=1221.126343f;
	wayPoints[58][1]=-589.808289f;
	wayPoints[58][2]=25.066719f;
	wayPoints[59][0]=1218.882813f;
	wayPoints[59][1]=-588.215271f;
	wayPoints[59][2]=25.517113f;
	wayPoints[60][0]=1216.414307f;
	wayPoints[60][1]=-587.058472f;
	wayPoints[60][2]=25.884275f;
	wayPoints[61][0]=1213.709961f;
	wayPoints[61][1]=-586.332092f;
	wayPoints[61][2]=26.143406f;
	wayPoints[62][0]=1210.872803f;
	wayPoints[62][1]=-586.057922f;
	wayPoints[62][2]=26.234367f;
	wayPoints[63][0]=1205.779663f;
	wayPoints[63][1]=-586.074829f;
	wayPoints[63][2]=25.756018f;
	wayPoints[64][0]=1200.881226f;
	wayPoints[64][1]=-586.066895f;
	wayPoints[64][2]=24.583145f;
	wayPoints[65][0]=1195.440918f;
	wayPoints[65][1]=-586.041077f;
	wayPoints[65][2]=22.687216f;
	wayPoints[66][0]=1180.597412f;
	wayPoints[66][1]=-586.029846f;
	wayPoints[66][2]=16.589781f;
	wayPoints[67][0]=1175.662354f;
	wayPoints[67][1]=-586.012634f;
	wayPoints[67][2]=14.922511f;
	wayPoints[68][0]=1170.660034f;
	wayPoints[68][1]=-585.997742f;
	wayPoints[68][2]=13.751955f;
	wayPoints[69][0]=1165.589355f;
	wayPoints[69][1]=-585.956482f;
	wayPoints[69][2]=13.307384f;
	wayPoints[70][0]=1159.970093f;
	wayPoints[70][1]=-585.926880f;
	wayPoints[70][2]=13.885757f;
	wayPoints[71][0]=1154.244629f;
	wayPoints[71][1]=-585.910217f;
	wayPoints[71][2]=15.470994f;
	wayPoints[72][0]=1148.606934f;
	wayPoints[72][1]=-585.894592f;
	wayPoints[72][2]=17.737175f;
	wayPoints[73][0]=1138.892334f;
	wayPoints[73][1]=-585.872864f;
	wayPoints[73][2]=22.470554f;
	wayPoints[74][0]=1131.735840f;
	wayPoints[74][1]=-585.859985f;
	wayPoints[74][2]=25.928331f;
	wayPoints[75][0]=1126.088257f;
	wayPoints[75][1]=-585.837463f;
	wayPoints[75][2]=28.199781f;
	wayPoints[76][0]=1120.578857f;
	wayPoints[76][1]=-585.834290f;
	wayPoints[76][2]=29.734301f;
	wayPoints[77][0]=1115.105469f;
	wayPoints[77][1]=-585.843262f;
	wayPoints[77][2]=30.306957f;
	wayPoints[78][0]=1112.536621f;
	wayPoints[78][1]=-586.081299f;
	wayPoints[78][2]=30.308937f;
	wayPoints[79][0]=1110.188110f;
	wayPoints[79][1]=-586.727112f;
	wayPoints[79][2]=30.307343f;
	wayPoints[80][0]=1108.003418f;
	wayPoints[80][1]=-587.764771f;
	wayPoints[80][2]=30.307928f;
	wayPoints[81][0]=1106.054199f;
	wayPoints[81][1]=-589.172058f;
	wayPoints[81][2]=30.305191f;
	wayPoints[82][0]=1104.360352f;
	wayPoints[82][1]=-590.867249f;
	wayPoints[82][2]=30.308365f;
	wayPoints[83][0]=1102.980591f;
	wayPoints[83][1]=-592.811829f;
	wayPoints[83][2]=30.305908f;
	wayPoints[84][0]=1101.951538f;
	wayPoints[84][1]=-594.994629f;
	wayPoints[84][2]=30.304945f;
	wayPoints[85][0]=1101.308594f;
	wayPoints[85][1]=-597.420654f;
	wayPoints[85][2]=30.307964f;
	wayPoints[86][0]=1101.085571f;
	wayPoints[86][1]=-599.917419f;
	wayPoints[86][2]=30.306244f;
	wayPoints[87][0]=1101.322266f;
	wayPoints[87][1]=-602.415833f;
	wayPoints[87][2]=30.308622f;
	wayPoints[88][0]=1101.971191f;
	wayPoints[88][1]=-604.800964f;
	wayPoints[88][2]=30.306707f;
	wayPoints[89][0]=1102.999023f;
	wayPoints[89][1]=-606.962585f;
	wayPoints[89][2]=30.306530f;
	wayPoints[90][0]=1104.394409f;
	wayPoints[90][1]=-608.931946f;
	wayPoints[90][2]=30.306345f;
	wayPoints[91][0]=1106.033081f;
	wayPoints[91][1]=-610.604980f;
	wayPoints[91][2]=30.294626f;
	wayPoints[92][0]=1108.268799f;
	wayPoints[92][1]=-611.761353f;
	wayPoints[92][2]=29.781843f;
	wayPoints[93][0]=1110.667603f;
	wayPoints[93][1]=-613.021606f;
	wayPoints[93][2]=28.322037f;
	wayPoints[94][0]=1112.886597f;
	wayPoints[94][1]=-614.197754f;
	wayPoints[94][2]=26.428659f;
	wayPoints[95][0]=1120.359619f;
	wayPoints[95][1]=-618.203247f;
	wayPoints[95][2]=19.168377f;
	wayPoints[96][0]=1122.908325f;
	wayPoints[96][1]=-619.606567f;
	wayPoints[96][2]=17.250757f;
	wayPoints[97][0]=1125.662842f;
	wayPoints[97][1]=-621.119080f;
	wayPoints[97][2]=15.885756f;
	wayPoints[98][0]=1128.563477f;
	wayPoints[98][1]=-622.725586f;
	wayPoints[98][2]=15.377464f;
	wayPoints[99][0]=1130.819336f;
	wayPoints[99][1]=-623.584595f;
	wayPoints[99][2]=15.332916f;
	wayPoints[100][0]=1133.061157f;
	wayPoints[100][1]=-624.085022f;
	wayPoints[100][2]=15.222240f;
	wayPoints[101][0]=1135.242798f;
	wayPoints[101][1]=-624.238831f;
	wayPoints[101][2]=15.081208f;
	wayPoints[102][0]=1137.334961f;
	wayPoints[102][1]=-624.106812f;
	wayPoints[102][2]=14.937264f;
	wayPoints[103][0]=1139.316284f;
	wayPoints[103][1]=-623.693176f;
	wayPoints[103][2]=14.824825f;
	wayPoints[104][0]=1141.217041f;
	wayPoints[104][1]=-623.041748f;
	wayPoints[104][2]=14.770391f;
	wayPoints[105][0]=1143.033325f;
	wayPoints[105][1]=-622.177673f;
	wayPoints[105][2]=14.806725f;
	wayPoints[106][0]=1144.689575f;
	wayPoints[106][1]=-621.169922f;
	wayPoints[106][2]=14.961944f;
	wayPoints[107][0]=1146.325928f;
	wayPoints[107][1]=-619.913269f;
	wayPoints[107][2]=15.294530f;
	wayPoints[108][0]=1157.421387f;
	wayPoints[108][1]=-610.716553f;
	wayPoints[108][2]=17.910954f;
	wayPoints[109][0]=1166.464355f;
	wayPoints[109][1]=-603.807434f;
	wayPoints[109][2]=20.501783f;
	wayPoints[110][0]=1175.622070f;
	wayPoints[110][1]=-597.740234f;
	wayPoints[110][2]=24.259220f;
	wayPoints[111][0]=1177.940063f;
	wayPoints[111][1]=-596.336365f;
	wayPoints[111][2]=25.293713f;
	wayPoints[112][0]=1181.922119f;
	wayPoints[112][1]=-594.204834f;
	wayPoints[112][2]=26.995760f;
	wayPoints[113][0]=1186.069580f;
	wayPoints[113][1]=-592.286377f;
	wayPoints[113][2]=28.656811f;
	wayPoints[114][0]=1190.267700f;
	wayPoints[114][1]=-590.649536f;
	wayPoints[114][2]=30.159908f;
	wayPoints[115][0]=1194.496826f;
	wayPoints[115][1]=-589.317505f;
	wayPoints[115][2]=31.437363f;
	wayPoints[116][0]=1198.871216f;
	wayPoints[116][1]=-588.340271f;
	wayPoints[116][2]=32.436985f;
	wayPoints[117][0]=1203.270264f;
	wayPoints[117][1]=-587.727478f;
	wayPoints[117][2]=33.079815f;
	wayPoints[118][0]=1207.762329f;
	wayPoints[118][1]=-587.507568f;
	wayPoints[118][2]=33.307919f;
	wayPoints[119][0]=1209.854126f;
	wayPoints[119][1]=-587.705078f;
	wayPoints[119][2]=33.308533f;
	wayPoints[120][0]=1211.777466f;
	wayPoints[120][1]=-588.235046f;
	wayPoints[120][2]=33.307568f;
	wayPoints[121][0]=1213.547485f;
	wayPoints[121][1]=-589.101929f;
	wayPoints[121][2]=33.305519f;
	wayPoints[122][0]=1215.149536f;
	wayPoints[122][1]=-590.232971f;
	wayPoints[122][2]=33.308441f;
	wayPoints[123][0]=1216.521362f;
	wayPoints[123][1]=-591.616638f;
	wayPoints[123][2]=33.305305f;
	wayPoints[124][0]=1217.647583f;
	wayPoints[124][1]=-593.233643f;
	wayPoints[124][2]=33.308407f;
	wayPoints[125][0]=1218.480957f;
	wayPoints[125][1]=-595.005066f;
	wayPoints[125][2]=33.308647f;
	wayPoints[126][0]=1219.026123f;
	wayPoints[126][1]=-596.956726f;
	wayPoints[126][2]=33.306534f;
	wayPoints[127][0]=1219.209961f;
	wayPoints[127][1]=-599.002563f;
	wayPoints[127][2]=33.308037f;
	wayPoints[128][0]=1219.018066f;
	wayPoints[128][1]=-601.079102f;
	wayPoints[128][2]=33.285095f;
	wayPoints[129][0]=1218.468872f;
	wayPoints[129][1]=-603.067139f;
	wayPoints[129][2]=33.207603f;
	wayPoints[130][0]=1217.607910f;
	wayPoints[130][1]=-604.839355f;
	wayPoints[130][2]=33.066811f;
	wayPoints[131][0]=1216.473755f;
	wayPoints[131][1]=-606.427979f;
	wayPoints[131][2]=32.848179f;
	wayPoints[132][0]=1215.101440f;
	wayPoints[132][1]=-607.783325f;
	wayPoints[132][2]=32.544090f;
	wayPoints[133][0]=1213.483765f;
	wayPoints[133][1]=-608.921387f;
	wayPoints[133][2]=32.126076f;
	wayPoints[134][0]=1211.649414f;
	wayPoints[134][1]=-609.796143f;
	wayPoints[134][2]=31.573099f;
	wayPoints[135][0]=1209.721191f;
	wayPoints[135][1]=-610.310425f;
	wayPoints[135][2]=30.910683f;
	wayPoints[136][0]=1207.668457f;
	wayPoints[136][1]=-610.468933f;
	wayPoints[136][2]=30.097921f;
	wayPoints[137][0]=1204.575439f;
	wayPoints[137][1]=-610.477539f;
	wayPoints[137][2]=28.285015f;
	wayPoints[138][0]=1202.020142f;
	wayPoints[138][1]=-610.537659f;
	wayPoints[138][2]=26.093721f;
	wayPoints[139][0]=1197.551758f;
	wayPoints[139][1]=-610.865601f;
	wayPoints[139][2]=21.128061f;
	wayPoints[140][0]=1195.374268f;
	wayPoints[140][1]=-611.195984f;
	wayPoints[140][2]=18.811777f;
	wayPoints[141][0]=1192.868286f;
	wayPoints[141][1]=-611.652832f;
	wayPoints[141][2]=16.683125f;
	wayPoints[142][0]=1189.831055f;
	wayPoints[142][1]=-612.256165f;
	wayPoints[142][2]=14.925616f;
	wayPoints[143][0]=1186.027832f;
	wayPoints[143][1]=-613.074585f;
	wayPoints[143][2]=13.734735f;
	wayPoints[144][0]=1181.476440f;
	wayPoints[144][1]=-614.117676f;
	wayPoints[144][2]=13.311397f;
	wayPoints[145][0]=1178.475586f;
	wayPoints[145][1]=-614.790710f;
	wayPoints[145][2]=13.666599f;
	wayPoints[146][0]=1175.807251f;
	wayPoints[146][1]=-615.446655f;
	wayPoints[146][2]=14.696528f;
	wayPoints[147][0]=1173.479248f;
	wayPoints[147][1]=-616.030945f;
	wayPoints[147][2]=16.227455f;
	wayPoints[148][0]=1171.318848f;
	wayPoints[148][1]=-616.588196f;
	wayPoints[148][2]=18.158102f;
	wayPoints[149][0]=1164.380737f;
	wayPoints[149][1]=-618.213440f;
	wayPoints[149][2]=25.448917f;
	wayPoints[150][0]=1161.609741f;
	wayPoints[150][1]=-618.733276f;
	wayPoints[150][2]=27.900717f;
	wayPoints[151][0]=1158.169922f;
	wayPoints[151][1]=-619.349304f;
	wayPoints[151][2]=30.370512f;
	wayPoints[152][0]=1154.971191f;
	wayPoints[152][1]=-619.845154f;
	wayPoints[152][2]=32.212734f;
	wayPoints[153][0]=1151.760498f;
	wayPoints[153][1]=-620.326172f;
	wayPoints[153][2]=33.686787f;
	wayPoints[154][0]=1148.649048f;
	wayPoints[154][1]=-620.710327f;
	wayPoints[154][2]=34.725029f;
	wayPoints[155][0]=1145.514160f;
	wayPoints[155][1]=-620.926819f;
	wayPoints[155][2]=35.331623f;
	wayPoints[156][0]=1142.344482f;
	wayPoints[156][1]=-620.986633f;
	wayPoints[156][2]=35.449890f;
	wayPoints[157][0]=1139.286743f;
	wayPoints[157][1]=-620.803833f;
	wayPoints[157][2]=35.048485f;
	wayPoints[158][0]=1136.160522f;
	wayPoints[158][1]=-620.330811f;
	wayPoints[158][2]=34.033489f;
	wayPoints[159][0]=1133.230713f;
	wayPoints[159][1]=-619.529297f;
	wayPoints[159][2]=32.480160f;
	wayPoints[160][0]=1130.299683f;
	wayPoints[160][1]=-618.356628f;
	wayPoints[160][2]=30.243128f;
	wayPoints[161][0]=1118.325073f;
	wayPoints[161][1]=-612.621826f;
	wayPoints[161][2]=19.758268f;
	wayPoints[162][0]=1115.906372f;
	wayPoints[162][1]=-611.462402f;
	wayPoints[162][2]=18.194792f;
	wayPoints[163][0]=1113.642456f;
	wayPoints[163][1]=-610.369934f;
	wayPoints[163][2]=17.052313f;
	wayPoints[164][0]=1111.263550f;
	wayPoints[164][1]=-609.200623f;
	wayPoints[164][2]=16.281528f;
	wayPoints[165][0]=1108.830078f;
	wayPoints[165][1]=-607.991943f;
	wayPoints[165][2]=16.008049f;
	wayPoints[166][0]=1107.453247f;
	wayPoints[166][1]=-606.700134f;
	wayPoints[166][2]=16.011156f;
	wayPoints[167][0]=1106.371582f;
	wayPoints[167][1]=-605.232849f;
	wayPoints[167][2]=16.011013f;
	wayPoints[168][0]=1105.553467f;
	wayPoints[168][1]=-603.605591f;
	wayPoints[168][2]=16.008368f;
	wayPoints[169][0]=1105.067383f;
	wayPoints[169][1]=-601.791809f;
	wayPoints[169][2]=16.010950f;
	wayPoints[170][0]=1104.868530f;
	wayPoints[170][1]=-599.866577f;
	wayPoints[170][2]=16.008410f;
	wayPoints[171][0]=1105.051392f;
	wayPoints[171][1]=-598.007324f;
	wayPoints[171][2]=16.011393f;
	wayPoints[172][0]=1105.537109f;
	wayPoints[172][1]=-596.261475f;
	wayPoints[172][2]=16.009300f;
	wayPoints[173][0]=1106.291382f;
	wayPoints[173][1]=-594.676758f;
	wayPoints[173][2]=16.008514f;
	wayPoints[174][0]=1107.292358f;
	wayPoints[174][1]=-593.267578f;
	wayPoints[174][2]=16.007708f;
	wayPoints[175][0]=1108.527710f;
	wayPoints[175][1]=-592.031006f;
	wayPoints[175][2]=16.010872f;
	wayPoints[176][0]=1109.999878f;
	wayPoints[176][1]=-590.983643f;
	wayPoints[176][2]=16.010813f;
	wayPoints[177][0]=1111.611572f;
	wayPoints[177][1]=-590.256226f;
	wayPoints[177][2]=16.010344f;
	wayPoints[178][0]=1113.336304f;
	wayPoints[178][1]=-589.782227f;
	wayPoints[178][2]=16.008802f;
	wayPoints[179][0]=1115.117188f;
	wayPoints[179][1]=-589.630615f;
	wayPoints[179][2]=16.007362f;
	wayPoints[180][0]=1118.556885f;
	wayPoints[180][1]=-589.733948f;
	wayPoints[180][2]=16.783937f;
	wayPoints[181][0]=1122.286987f;
	wayPoints[181][1]=-589.827515f;
	wayPoints[181][2]=18.897638f;
	wayPoints[182][0]=1126.185181f;
	wayPoints[182][1]=-589.900330f;
	wayPoints[182][2]=21.883369f;
	wayPoints[183][0]=1134.397705f;
	wayPoints[183][1]=-589.948181f;
	wayPoints[183][2]=28.982338f;
	wayPoints[184][0]=1139.056274f;
	wayPoints[184][1]=-589.962952f;
	wayPoints[184][2]=32.683502f;
	wayPoints[185][0]=1143.421265f;
	wayPoints[185][1]=-589.955994f;
	wayPoints[185][2]=35.545670f;
	wayPoints[186][0]=1147.927979f;
	wayPoints[186][1]=-589.926697f;
	wayPoints[186][2]=37.556583f;
	wayPoints[187][0]=1152.505249f;
	wayPoints[187][1]=-589.892151f;
	wayPoints[187][2]=38.301643f;
	wayPoints[188][0]=1156.544800f;
	wayPoints[188][1]=-589.862244f;
	wayPoints[188][2]=38.014900f;
	wayPoints[189][0]=1160.785278f;
	wayPoints[189][1]=-589.830444f;
	wayPoints[189][2]=37.173004f;
	wayPoints[190][0]=1164.921265f;
	wayPoints[190][1]=-589.781189f;
	wayPoints[190][2]=35.921066f;
	wayPoints[191][0]=1168.788696f;
	wayPoints[191][1]=-589.739197f;
	wayPoints[191][2]=34.447453f;
	wayPoints[192][0]=1172.919678f;
	wayPoints[192][1]=-589.676453f;
	wayPoints[192][2]=32.629063f;
	wayPoints[193][0]=1183.125732f;
	wayPoints[193][1]=-589.603760f;
	wayPoints[193][2]=27.836641f;
	wayPoints[194][0]=1186.251221f;
	wayPoints[194][1]=-589.628662f;
	wayPoints[194][2]=26.587688f;
	wayPoints[195][0]=1187.907227f;
	wayPoints[195][1]=-589.775146f;
	wayPoints[195][2]=26.052610f;
	wayPoints[196][0]=1189.546509f;
	wayPoints[196][1]=-590.236145f;
	wayPoints[196][2]=25.579178f;
	wayPoints[197][0]=1191.007935f;
	wayPoints[197][1]=-590.918396f;
	wayPoints[197][2]=25.196878f;
	wayPoints[198][0]=1192.317871f;
	wayPoints[198][1]=-591.865967f;
	wayPoints[198][2]=24.878492f;
	wayPoints[199][0]=1193.470947f;
	wayPoints[199][1]=-593.002075f;
	wayPoints[199][2]=24.629271f;
	wayPoints[200][0]=1194.373657f;
	wayPoints[200][1]=-594.314636f;
	wayPoints[200][2]=24.444614f;
	wayPoints[201][0]=1195.072632f;
	wayPoints[201][1]=-595.780334f;
	wayPoints[201][2]=24.316750f;
	wayPoints[202][0]=1195.512817f;
	wayPoints[202][1]=-597.421021f;
	wayPoints[202][2]=24.241983f;
	wayPoints[203][0]=1195.654785f;
	wayPoints[203][1]=-599.091003f;
	wayPoints[203][2]=24.217606f;
	wayPoints[204][0]=1195.534546f;
	wayPoints[204][1]=-600.398987f;
	wayPoints[204][2]=24.144957f;
	wayPoints[205][0]=1195.208618f;
	wayPoints[205][1]=-601.701599f;
	wayPoints[205][2]=23.953278f;
	wayPoints[206][0]=1194.759888f;
	wayPoints[206][1]=-602.782898f;
	wayPoints[206][2]=23.718634f;
	wayPoints[207][0]=1194.163086f;
	wayPoints[207][1]=-603.959900f;
	wayPoints[207][2]=23.422689f;
	wayPoints[208][0]=1191.085449f;
	wayPoints[208][1]=-608.930176f;
	wayPoints[208][2]=22.280317f;
	wayPoints[209][0]=1187.565918f;
	wayPoints[209][1]=-614.025146f;
	wayPoints[209][2]=21.128988f;
	wayPoints[210][0]=1185.060547f;
	wayPoints[210][1]=-617.184265f;
	wayPoints[210][2]=20.455950f;
	wayPoints[211][0]=1183.319214f;
	wayPoints[211][1]=-619.083557f;
	wayPoints[211][2]=20.091845f;
	wayPoints[212][0]=1182.030762f;
	wayPoints[212][1]=-620.363281f;
	wayPoints[212][2]=19.881638f;
	wayPoints[213][0]=1180.580933f;
	wayPoints[213][1]=-621.632813f;
	wayPoints[213][2]=19.735582f;
	wayPoints[214][0]=1179.242432f;
	wayPoints[214][1]=-622.614319f;
	wayPoints[214][2]=19.685040f;
	wayPoints[215][0]=1175.935059f;
	wayPoints[215][1]=-624.295715f;
	wayPoints[215][2]=19.461813f;
	wayPoints[216][0]=1172.918091f;
	wayPoints[216][1]=-624.767700f;
	wayPoints[216][2]=18.877224f;
	wayPoints[217][0]=1170.179199f;
	wayPoints[217][1]=-624.282593f;
	wayPoints[217][2]=18.043154f;
	wayPoints[218][0]=1167.586426f;
	wayPoints[218][1]=-623.027527f;
	wayPoints[218][2]=17.014887f;
	wayPoints[219][0]=1165.255127f;
	wayPoints[219][1]=-621.317627f;
	wayPoints[219][2]=15.966480f;
	wayPoints[220][0]=1162.931030f;
	wayPoints[220][1]=-619.310547f;
	wayPoints[220][2]=14.939217f;
	wayPoints[221][0]=1160.712158f;
	wayPoints[221][1]=-617.341492f;
	wayPoints[221][2]=14.105062f;
	wayPoints[222][0]=1158.426758f;
	wayPoints[222][1]=-615.549500f;
	wayPoints[222][2]=13.525311f;
	wayPoints[223][0]=1156.062622f;
	wayPoints[223][1]=-614.158325f;
	wayPoints[223][2]=13.306892f;
	wayPoints[224][0]=1154.351929f;
	wayPoints[224][1]=-613.582947f;
	wayPoints[224][2]=13.308118f;
	wayPoints[225][0]=1152.153320f;
	wayPoints[225][1]=-613.287354f;
	wayPoints[225][2]=13.305757f;
	wayPoints[226][0]=1149.513062f;
	wayPoints[226][1]=-613.140808f;
	wayPoints[226][2]=13.307752f;
	wayPoints[227][0]=1143.483643f;
	wayPoints[227][1]=-613.109436f;
	wayPoints[227][2]=13.308438f;
	wayPoints[228][0]=1140.098877f;
	wayPoints[228][1]=-613.050659f;
	wayPoints[228][2]=13.306764f;
	wayPoints[229][0]=1136.510498f;
	wayPoints[229][1]=-612.835388f;
	wayPoints[229][2]=13.306905f;
	wayPoints[230][0]=1133.042969f;
	wayPoints[230][1]=-612.415527f;
	wayPoints[230][2]=13.308578f;
	wayPoints[231][0]=1129.482178f;
	wayPoints[231][1]=-611.668457f;
	wayPoints[231][2]=13.305981f;

	while(IsThreadAlive())
	{
		Wait(0);
		if(TimerA()>=3000)
			currentText="";
		//SetTextFont(3);
		DisplayTextWithLiteralString(0.5f-(GetStringWidthWithString("STRING",currentText)/2),0.4f,"STRING",currentText);
		
		if ((GetAsyncKeyState(VK_DELETE) & 0x8000))
		{
			modStarted=!modStarted;
			if(modStarted)
			{
				LogInfo("StartMod");
				SetTimerA(0);
				currentText = "Enabling the Roller-Coaster Mod made by Maddy25 !";
				eModel model=MODEL_AIRTUG;
				RequestModel(model);
				while(!HasModelLoaded(model))
					{
						Wait(0);
					}
				LogInfo("Car model available... spawning it!");
				CreateCar(model, wayPoints[0][0],wayPoints[0][1] , wayPoints[0][2], &rollerCoasterCar,1);
				MarkModelAsNoLongerNeeded(model);
				model=CJ_DOG_TIN_2;
				RequestModel(model);
				while(!HasModelLoaded(model))
					{
						Wait(0);
					}
				CreateObject(model,wayPoints[0][0],wayPoints[0][1] , wayPoints[0][2],&modelObject,1);
				CreateObject(model,0.0f,0.0f,0.0f,&helpObject,1);
				CreateObject(model,0.0f,0.0f,0.0f,&helpObject2,1);
				SetEngineHealth(rollerCoasterCar,0.0f);
				SetCarCanBeDamaged(rollerCoasterCar,0);
				MarkModelAsNoLongerNeeded(model);

				modStarted=1;
				nextX=wayPoints[0][0];
				nextY=wayPoints[0][1];
				nextZ=wayPoints[0][2];
				doubleNextX=wayPoints[1][0];
				doubleNextY=wayPoints[1][1];
				doubleNextZ=wayPoints[1][2];
				newPartialRoad();
				Wait(200);
			}
			else
			{
				roundRunning=0;
				SetTimerA(0);
				currentText = "Disabling the Roller-Coaster Mod made by Maddy25 !";
				DeleteCar(&rollerCoasterCar);
				DeleteObject(&modelObject);
				DeleteObject(&helpObject);
				DeleteObject(&helpObject2);
				modStarted=0;
				Wait(200);
			}
		}
		if(modStarted){
			if ((GetAsyncKeyState(VK_END) & 0x8000))
			{
				roundRunning=!roundRunning;
				wayPointIndex=1;
				nextX=wayPoints[0][0];
				nextY=wayPoints[0][1];
				nextZ=wayPoints[0][2];
				doubleNextX=wayPoints[1][0];
				doubleNextY=wayPoints[1][1];
				doubleNextZ=wayPoints[1][2];
				newPartialRoad();
				SetTimerA(0);
				currentText = "Toggle Start/Stop Round!";
				Wait(200);
			}
			if(roundRunning)
			{
				if(distanceCounter>distance)
					newPartialRoad();
				f32 lastX=newX;
				f32 lastY=newY;
				currentX+=vectorX;
				currentY+=vectorY;
				currentZ+=vectorZ;
				f32 currentDistance = sqrt(pow(nX-currentX,2)+pow(nY-currentY,2)+pow(nZ-currentZ,2));
				newZ=currentDistance*currentZ/radius2;

				currentAngle += (totalAngle*speed/distance);
				currentAngle2 += (totalAngle2*speed/distance);

				newX=mX+sin(currentAngle)*radius;
				newY=mY+cos(currentAngle)*radius;
				//currentZ=nZ+cos(currentAngle2)*radius2;
				//currentX+=vectorX;
				//currentY+=vectorY;
				//currentZ+=vectorZ;
				rotZ=atan2(newY-lastY,newX-lastX)*180/M_PI-90;
				distanceCounter+=speed;
				SetObjectCoordinates(modelObject,newX,newY,newZ);
				SetObjectRotation(modelObject,rotX,0.0f,rotZ);
				AttachCarToObject(rollerCoasterCar,modelObject,0,0.0f, 0.0f, 0.0f, 0.0f,0.0f,0.0f);
			}
			else{
				DetachCar(rollerCoasterCar);
				SetObjectCoordinates(modelObject,currentX,currentY,currentZ);
				SetObjectRotation(modelObject,rotX,0.0f,rotZ);
				SetCarCoordinates(rollerCoasterCar,currentX,currentY,currentZ);
				SetCarHeading(rollerCoasterCar,rotZ);
				//SetObjectCoordinates(helpObject,helpX,helpY,helpZ);		//this is just to debug for the middlepoint
			}
		}
	}
}
